//
//  MCPAppUtils.m
//  mcp-common
//
//  Created by 艽野尘梦 on 2021/3/25.
//  Copyright © 2021 mcp. All rights reserved.
//

#import "MCPAppUtils.h"
#import <UIKit/UIKit.h>

@implementation MCPAppUtils

+ (NSString *)getBundleId {
    
    return [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleIdentifier"]?:@"";

}

+ (NSString *)getAppName {
    
    NSString *displayName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"];
    if (displayName) {
        return displayName;
    }
    
    NSString *bundleName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"];
    if (bundleName) {
        return bundleName;
    }
    
    NSString *executableName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleExecutable"];
    if (executableName) {
        return executableName;
    }
    
    return nil;
}

+ (NSString *)getAppVersion {
    
    return [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]?:@"";
    
}

+ (NSString *)getAppBuildVersion {
    
    return [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]?:@"";
    
}

+ (NSString *)getAppIconBase64 {
    
    NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
    NSString *icon = [[infoDict valueForKeyPath:@"CFBundleIcons.CFBundlePrimaryIcon.CFBundleIconFiles"] lastObject];
    UIImage *img = [UIImage imageNamed:icon];
    NSData *image = UIImagePNGRepresentation(img);
    NSString *encodedImageStr = [image base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
    encodedImageStr = [encodedImageStr stringByReplacingOccurrencesOfString:@"\r" withString:@""];
    encodedImageStr = [encodedImageStr stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    encodedImageStr = [NSString stringWithFormat:@"%@%@",@"data:image/png;base64,",encodedImageStr];

    return encodedImageStr?:@"";
}

@end
